package mail.verify.slider;

import mail.entity.ProxyEntity;
import mail.fetch.qq.Login11;
import mail.proxy.ProxyManager;
import mail.util.FileManager;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static com.han.http.util.Print.printlnInfo;

/**
 * @author: Hanjiafeng
 * @date: Created in 14:44 2019/3/4
 * @desc:
 */
public class ThirdPartTest {

    public static void main(String... args) {
        ThirdPartTest thirdPartTest = new ThirdPartTest();
        thirdPartTest.testSuccessRate();
    }

    private void testSuccessRate() {
        List<String[]> qqList = readAvailableQq();
        int successCount = 0;
        int failedCount = 0;
        int c = 0;
        for (String[] qq : qqList) {
            /*if (c++<0){
                continue;
            }*/
            try {
                boolean isSuccess = test(qq[0], qq[1]);
                if (isSuccess) {
                    successCount++;
                    printlnInfo("成功:" + successCount);
                } else {
                    failedCount++;
                    printlnInfo("失败:" + failedCount);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean test(String account, String password) {
        boolean isSuccess = false;
        ProxyEntity proxyEntity = ProxyManager.getProxy();
        Login11 login = new Login11("", proxyEntity);
        printlnInfo("账号:" + account + ",密码:" + password + ",代理:" + proxyEntity);
        try {
            login.getCookieManager(account, password);
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ProxyManager.returnProxy(proxyEntity, 1);
        }

        return isSuccess;
    }

    /**
     * 读取可用QQ号
     *
     * @return QQ account list
     */
    private List<String[]> readAvailableQq() {
        List<String[]> qqList = new ArrayList<>();
        try {
            String content = FileManager.read("F:\\another\\AvailableQQ.txt", Charset.forName("UTF-8"));
            String[] qqs = content.split("\n");
            for (String qq : qqs) {
                String[] q = qq.split(",");
                qqList.add(q);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return qqList;
    }
}
